package com.zbo.service;

import com.zbo.mapper.ScMapper;
import com.zbo.pojo.Sc;
import com.zbo.util.SqlSessionFactoryUtils;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import java.util.List;

public class ScService {
    SqlSessionFactory sqlSessionFactory = SqlSessionFactoryUtils.getSqlSessionFactory();

    // 根据学生id和周数获取课程
    public List<Sc> getTimetableByStudentIdAndWeek(Integer studentId, Integer currentWeek) {
        try (SqlSession session = sqlSessionFactory.openSession()) {
            ScMapper mapper = session.getMapper(ScMapper.class);
            return mapper.selectByStudentIdAndWeek(studentId, currentWeek);
        }
    }

    // 获取学生所有课程
    public List<Sc> getTimetableByStudentId(Integer studentId) {
        try (SqlSession session = sqlSessionFactory.openSession()) {
            ScMapper mapper = session.getMapper(ScMapper.class);
            return mapper.selectAllByStudentId(studentId);
        }
    }

    // 添加课程
    public boolean addCourse(Sc sc) {
        try (SqlSession session = sqlSessionFactory.openSession()) {
            ScMapper mapper = session.getMapper(ScMapper.class);
            int result = mapper.insert(sc);
            session.commit();
            return result > 0;
        }
    }

    // 删除课程
    public boolean deleteCourse(Integer scId) {
        try (SqlSession session = sqlSessionFactory.openSession()) {
            ScMapper mapper = session.getMapper(ScMapper.class);
            int result = mapper.deleteById(scId);
            session.commit();
            return result > 0;
        }
    }

    // 编辑课程
    public boolean updateCourse(Sc sc) {
        try (SqlSession session = sqlSessionFactory.openSession()) {
            ScMapper mapper = session.getMapper(ScMapper.class);
            int result = mapper.update(sc);
            session.commit();
            return result > 0;
        }
    }

    //根据id和时间查询
    public List<Sc> getCoursesByStudentIdAndTime(Integer studentId, Integer dayOfWeek, String startTime) {
        try (SqlSession session = sqlSessionFactory.openSession()) {
            ScMapper mapper = session.getMapper(ScMapper.class);
            return mapper.selectByStudentIdAndTime(studentId, dayOfWeek, startTime);
        }
    }
}
